package class4;

/**
 * https://leetcode.cn/problems/binary-number-with-alternating-bits/description/
 * 交替位二进制数
 */
public class Code06_hasAlternatingBits {

    public static void main(String[] args) {
        int num = 10;
        System.out.println(hasAlternatingBits(num)); // 输出 "10101"
    }

    public static boolean hasAlternatingBits(int n) {
        /**
         * 如果是
         *       01010101
         *     ^ 00101010
         *       01111111  + 1 =  10000000
         *     & 10000000
         *     = 00000000
         * 当结果完全为0时,则说明是交替位的二进制数
         */
        int x = n ^ (n >> 1);
        return (x & (x + 1)) == 0;
    }
}
